/**
 *
 * @Copyright (c) 2015 Messcat. All rights reserved.
 */
package cc.messcat.service.system;

import cc.messcat.common.constant.CommonConstant;
import cc.messcat.common.model.Pager;
import cc.messcat.common.service.BaseServiceImpl;
import cc.messcat.common.util.BeanUtil;
import cc.messcat.common.util.CollectionUtil;
import cc.messcat.dao.area.AreaDao;
import cc.messcat.dao.system.PermissionDao;
import cc.messcat.dao.system.RoleDao;
import cc.messcat.entity.area.Area;
import cc.messcat.entity.system.Permission;
import cc.messcat.entity.system.Role;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author Panda
 * @version 1.0
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService {

	@Autowired
	private RoleDao roleDao;

	@Autowired
	private PermissionDao permissionDao;
	
//	@Autowired
//	private AreaDao areaDao;

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * cc.messcat.service.system.RoleService#addRole(cc.messcat.entity.system.
	 * Role)
	 */
	@Override
	public void addRole(Role role, Long[] permissionss){
		// 无权限
		List<Permission> permissionList = permissionDao.find(permissionss);
		for(Long id:permissionss){
			List<Permission> PermissionType4 = permissionDao.findPermissionType4(id);
			permissionList.addAll(PermissionType4);
		}
		if (CollectionUtil.isListNotEmpty(permissionList)) {
			Set<Permission> permissionSet = new HashSet<>(permissionList);
			role.setPermissions(permissionSet);
		}
		this.addObject(role);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * cc.messcat.service.system.RoleService#modifyRole(cc.messcat.entity.system
	 * .Role)
	 */
	@Override
	public void modifyRole(Role role, Long[] permissionss){
		Role role0 = this.retrieveObject(role.getId());
		if (null == role0)
			throw new NullPointerException(CommonConstant.MSG_ROLE_EXCEPTION);

		List<Permission> permissionList = permissionDao.find(permissionss);

		for(Long id:permissionss){
			List<Permission> PermissionType4 = permissionDao.findPermissionType4(id);
			permissionList.addAll(PermissionType4);
		}

		if (CollectionUtil.isListNotEmpty(permissionList)) {
			Set<Permission> permissionSet = new HashSet<>(permissionList);
			role0.setPermissions(permissionSet);
		} else {
			role0.setPermissions(new HashSet<Permission>());
		}
		
		role0.setStatus(role.getStatus());
		role0.setDescription(role.getDescription());
		role0.setName(role.getName());
		this.modifyObject(role0);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see cc.messcat.service.system.RoleService#removeRole(java.lang.Long)
	 */
	@Override
	public Long removeRole(Long id) {
		Role role = roleDao.get(id);
		if(role != null && role.getUsers().isEmpty()){
			removeObject(id);
			return id;
		} 
		return -1L;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * cc.messcat.service.system.RoleService#retrieveRoleById(java.lang.Long)
	 */
	@Override
	public Role retrieveRoleById(Long id) {
		Role role = this.retrieveObject(id);
		Role roleVo = new Role();
		if(BeanUtil.isValid(role)){
			roleVo.setId(role.getId());
			roleVo.setName(role.getName());
			roleVo.setDescription(role.getDescription());
			roleVo.setStatus(role.getStatus());
			Set<Permission> permsVo = new HashSet<>();
			Set<Permission> perms = role.getPermissions();
			for (Permission p : perms) {
				if (null != p.getPermissionType() && !p.getPermissionType().equals("4")) {
					permsVo.add(p);
				}
			}
			roleVo.setPermissions(permsVo);
		}
		return roleVo;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * cc.messcat.service.system.RoleService#retrieveRoleAll(java.lang.Integer,
	 * java.lang.Integer)
	 */
	@Override
	public Pager retrieveRoleAll(Integer pageNo, Integer pageSize) {
		List<Role> list = retrieveRoleAll();
		List<Role> roles = new ArrayList<>();
		for(Role r:list){
			Role role = new Role();
			Set<Permission> perms = r.getPermissions();
			Set<Permission> permsVo = new HashSet<>();
			if(BeanUtil.isValid(r.getPermissions())){
				for (Permission p : perms) {
					if (null != p.getPermissionType() && !p.getPermissionType().equals("4")) {
						permsVo.add(p);
					}
				}

			}
			role.setId(r.getId());
			role.setName(r.getName());
			role.setStatus(r.getStatus());
			role.setDescription(r.getDescription());
//			role.setAreaId(r.getAreaId());
//			role.setRoleLevel(r.getRoleLevel());
//			if(0L!=r.getAreaId()){
//				Area area = areaDao.getByID(r.getAreaId());
//				role.setArea(area);
//			}
			if(BeanUtil.isValid(permsVo)){
				role.setPermissions(permsVo);
			}
			roles.add(role);
		}
		return getPager(pageNo, pageSize, roles, 100);
	}

	private List<Role> retrieveRoleAll() {
		return this.retrieveAllObjects();
	}

	/**
	 * 是否存在角色
	 */
	@Override
	public boolean isExistRoles(String roles, Long id) {
		Role role=roleDao.getRoleByName(roles);
		if(BeanUtil.isValid(role)){
			if(BeanUtil.isValid(id)){
				if(Objects.equals(role.getId(), id)){
					return true;
				}
			}else{
				return false;
			}
		}else{
			return true;
		}
		return false;
	}

	@Override
	public Pager<Role> retrieveRoleAllFilter(Integer pageNo, Integer pageSize) {
		List<Role> list = roleDao.retrieveRoleAllFilter("0");
		List<Role> roles = new ArrayList<>();
		for(Role r:list){
			Role role = new Role();
			Set<Permission> perms = r.getPermissions();
			Set<Permission> permsVo = new HashSet<>();
			if(BeanUtil.isValid(r.getPermissions())){
				for (Permission p : perms) {
					if (null != p.getPermissionType() && !p.getPermissionType().equals("4")) {
						permsVo.add(p);
					}
				}

			}
			role.setId(r.getId());
			role.setName(r.getName());
			role.setStatus(r.getStatus());
			role.setDescription(r.getDescription());
			if(BeanUtil.isValid(permsVo)){
				role.setPermissions(permsVo);
			}
			roles.add(role);
		}
		return getPager(pageNo, pageSize, roles, 100);
	}

//	@Override
//	public List<Role> retrieveRoleByArea(Area area) {
//		return roleDao.retrieveRoleByArea(area);
//	}

}
